package com.autumn.auditing;

/**
 * 客户端浏览器信息
 * 
 * @author 杨昌国
 *
 *         2017-10-20 13:37:30
 */
public class ClientBrowserInfo implements IClientBrowserInfo {

	private final static String BROWSER_EDGE = "Edge";
	private final static String BROWSER_MSIE = "MSIE";
	private final static String BROWSER_IE = "IE";
	private final static String BROWSER_RV = "RV";
	private final static String BROWSER_SAFARI = "Safari";
	private final static String BROWSER_OPERA = "Opera";
	private final static String BROWSER_OPR = "OPR";
	private final static String BROWSER_CHROME = "Chrome";
	private final static String BROWSER_FIREFOX = "Firefox";
	private final static String BROWSER_MOZILLA = "Mozilla";
	private final static String BROWSER_NETSCAPE = "Netscape";
	
	private final static String OPERATING_SYSTEM_WINDOWS = "Windows";
	private final static String OPERATING_SYSTEM_MAC = "Mac";
	private final static String OPERATING_SYSTEM_X11 = "X11";
	private final static String OPERATING_SYSTEM_ANDROID = "Android";
	private final static String OPERATING_SYSTEM_IPHONE = "iphone";
	private final static String OPERATING_SYSTEM_IPAD = "ipad";
	private final static String OPERATING_SYSTEM_LINUX = "linux";
	private final static String OPERATING_SYSTEM_UNIX= "Unix";
	
	/**
	 * 实例
	 */
	public static final IClientBrowserInfo INSTANCE = new ClientBrowserInfo("");

	private final String browserInfo;

	/**
	 * 
	 * @param browserInfo
	 */
	public ClientBrowserInfo(String browserInfo) {
		this.browserInfo = browserInfo != null ? browserInfo : "";
	}

	/**
	 * 获取浏览器信息
	 * 
	 * @return
	 */
	@Override
	public String getBrowserInfo() {
		return browserInfo;
	}

	private String browserName = null;

	private final static String BROWSER_VERSION = "version";



	/**
	 * 获取浏览器名称
	 * 
	 * @return
	 */
	@Override
	public String getBrowserName() {
		if (browserName != null) {
			return browserName;
		}
		if (browserInfo.contains(BROWSER_EDGE)) {
			browserName = (browserInfo.substring(browserInfo.indexOf(BROWSER_EDGE)).split(" ")[0]).replace("/", "-");
		} else if (browserInfo.contains(BROWSER_MSIE)) {
			String substring = browserInfo.substring(browserInfo.indexOf(BROWSER_MSIE)).split(";")[0];
			browserName = substring.split(" ")[0].replace(BROWSER_MSIE, BROWSER_IE) + "-" + substring.split(" ")[1];
		} else if (browserInfo.contains(BROWSER_SAFARI) && browserInfo.contains(BROWSER_VERSION)) {
			browserName = (browserInfo.substring(browserInfo.indexOf(BROWSER_SAFARI)).split(" ")[0]).split("/")[0] + "-"
					+ (browserInfo.substring(browserInfo.indexOf("Version")).split(" ")[0]).split("/")[1];
		} else if (browserInfo.contains(BROWSER_OPR) || browserInfo.contains(BROWSER_OPERA)) {
			if (browserInfo.contains(BROWSER_OPERA)) {
				browserName = (browserInfo.substring(browserInfo.indexOf(BROWSER_OPERA)).split(" ")[0]).split("/")[0]
						+ "-" + (browserInfo.substring(browserInfo.indexOf("Version")).split(" ")[0]).split("/")[1];
			} else if (browserInfo.contains(BROWSER_OPR)) {
				browserName = ((browserInfo.substring(browserInfo.indexOf(BROWSER_OPR.toUpperCase())).split(" ")[0])
						.replace("/", "-")).replace(BROWSER_OPR.toUpperCase(), BROWSER_OPERA);
			}
		} else if (browserInfo.contains(BROWSER_CHROME)) {
			browserName = (browserInfo.substring(browserInfo.indexOf(BROWSER_CHROME)).split(" ")[0]).replace("/", "-");
		} else if ((browserInfo.indexOf(BROWSER_MOZILLA) > -1) || (browserInfo.indexOf(BROWSER_NETSCAPE) != -1)) {
			browserName = "Netscape-?";
		} else if (browserInfo.contains(BROWSER_FIREFOX)) {
			browserName = (browserInfo.substring(browserInfo.indexOf(BROWSER_FIREFOX)).split(" ")[0]).replace("/", "-");
		} else if (browserInfo.contains(BROWSER_RV)) {
			String ieVersion = (browserInfo.substring(browserInfo.indexOf(BROWSER_RV)).split(" ")[0])
					.replace(BROWSER_RV + ":", "-");
			browserName = BROWSER_IE + ieVersion.substring(0, ieVersion.length() - 1);
		} else {
			browserName = "";
		}
		return browserName;
	}

	private String browserPlatformName = null;

	

	/**
	 * 获取浏览器对应的平台
	 * 
	 * @return
	 */
	@Override
	public String getBrowserPlatform() {
		if (browserPlatformName != null) {
			return browserPlatformName;
		}
		String value = browserInfo.toLowerCase();
		if (value.indexOf(OPERATING_SYSTEM_WINDOWS.toLowerCase()) > 0) {
			browserPlatformName = OPERATING_SYSTEM_WINDOWS;
		} else if (value.indexOf(OPERATING_SYSTEM_MAC.toLowerCase()) > 0) {
			browserPlatformName = OPERATING_SYSTEM_MAC;
		} else if (value.indexOf(OPERATING_SYSTEM_X11.toLowerCase()) > 0) {
			browserPlatformName = OPERATING_SYSTEM_UNIX;
		} else if (value.indexOf(OPERATING_SYSTEM_ANDROID) > 0) {
			browserPlatformName = OPERATING_SYSTEM_ANDROID;
		} else if (value.indexOf(OPERATING_SYSTEM_IPHONE) > 0) {
			browserPlatformName = OPERATING_SYSTEM_IPHONE;
		} else if (value.indexOf(OPERATING_SYSTEM_IPAD) > 0) {
			browserPlatformName = OPERATING_SYSTEM_IPAD;
		} else if (value.indexOf(OPERATING_SYSTEM_LINUX.toLowerCase()) > 0) {
			browserPlatformName = OPERATING_SYSTEM_LINUX;
		} else {
			browserPlatformName = "";
		}
		return browserPlatformName;
	}
}
